package one.one.twentyTwo;

public class BinarySearchRecursion {

	public static int rank(int key, int[] a, int lo, int hi) {
		if (lo > hi)
			return -1;
		int mid = lo + (hi - lo) / 2;
		if (key < a[mid])
			return rank(key, a, lo, mid - 1);
		else if (key > a[mid])
			return rank(key, a, mid + 1, hi);
		else
			return mid;
	}

	static int binarySearchRec(int k, int[] a, int l, int h) {
		System.out.println("low:" + l + ",high:" + h);
		if (l > h) 	
			return -1;
		
		int mid = l + (h - l) / 2;
		if (k < a[mid])
			return binarySearchRec(k, a, l, mid - 1);
		else if (k > a[mid])
			return binarySearchRec(k, a, mid + 1, h);
		else
			return mid;
	}

	static int binarySearch(int k, int[] a) {
		return binarySearchRec(k, a, 0, a.length - 1);
	}

	public static void main(String[] args) {
		int[] a = { 1, 2, 3, 4, 5, 6, 7 };
		int r=binarySearch(3, a);
		System.out.println(r);

	}

}
